package com.example.lianxi222.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lianxi222.R;
import com.example.lianxi222.adapter.TabAdapter;
import com.example.lianxi222.adapter.Vp2Adapter;
import com.example.lianxi222.databinding.FragmentHomeBinding;
import com.example.lianxi222.fragment.home.FirstFragment;
import com.example.lianxi222.fragment.home.HomeViewModel;
import com.example.mvvm_base.view.BaseFragment;
import com.example.mvvm_base.view.BaseSimpleFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseSimpleFragment<FragmentHomeBinding> {
 private ArrayList<CustomTabEntity> tabList=new ArrayList<>();
 private List<Fragment> fragmentList=new ArrayList<>();
 private Vp2Adapter vp2Adapter;



    @Override
    protected void initData() {
        tabList.clear();
        fragmentList.clear();
        tabList.add(new TabAdapter("关注"));
        tabList.add(new TabAdapter("推荐"));
        tabList.add(new TabAdapter("同城"));
        for (int i = 0; i < tabList.size(); i++) {
            fragmentList.add(new FirstFragment());
        }
        vp2Adapter=new Vp2Adapter(requireActivity(),fragmentList);
        dataBinding.table.setTabData(tabList);
        dataBinding.homeVp2.setAdapter(vp2Adapter);



    }

    @Override
    protected void initView() {
    dataBinding.table.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            dataBinding.homeVp2.setCurrentItem(position);
        }

        @Override
        public void onTabReselect(int position) {

        }
    });

   dataBinding.homeVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
       @Override
       public void onPageSelected(int position) {
           super.onPageSelected(position);
           dataBinding.table.setCurrentTab(position);
       }
   });

    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}